import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:library_base/entities/enum_entity.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/utils/widget_generate_util.dart';
import 'package:library_base/utils/get_util.dart';

class DynamicOptionDialog extends StatefulWidget {
  const DynamicOptionDialog({
    Key? key,
  }) : super(key: key);

  @override
  State<DynamicOptionDialog> createState() => _DynamicOptionDialogState();
}

class _DynamicOptionDialogState extends State<DynamicOptionDialog> {
  void Function(String)? callback;

  List<String> actionStr = [];

  String cancelStr = MultiLanguage.cancel.tr;

  @override
  void initState() {
    super.initState();
    var arguments = Get.arguments;
    callback = arguments['CallBack'];
    actionStr = arguments['actionStr'];
    cancelStr = arguments['cancelStr'] ?? MultiLanguage.cancel.tr;
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        GetHelper.back();
        return Future.value(false);
      },
      child: GestureDetector(
        onTap: () {
          GetHelper.back();
        },
        child: Container(
          color: Colors.transparent,
          child: GenerateWidgetUtil.roundWidget(
              width: double.infinity,
              alignment: Alignment.bottomCenter,
              child: Container(
                width: double.infinity,
                clipBehavior: Clip.hardEdge,
                decoration: BoxDecoration(
                  color: ThemeUtil.getTheme().colorScheme.secondary,
                  borderRadius: const BorderRadius.only(
                      topLeft: Radius.circular(10.0), topRight: Radius.circular(10.0)),
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: buildColumn(),
                ),
              )),
        ),
      ),
    );
  }

  List<Widget> buildColumn() {
    List<Widget> widgets = [];
    widgets.addAll(buildOption());
    widgets.add(line2);
    widgets.add(buildCancelItem());
    return widgets;
  }

  List<Widget> buildOption() {
    return actionStr.buildItem((index, element) {
      return GestureDetector(
        onTap: () {
          callback?.call(element);
        },
        child: Container(
          color: Colors.transparent,
          alignment: Alignment.center,
          width: double.infinity,
          height: 99.w,
          child: AutoSizeText(
            element,
            style: TextStyle(fontSize: 30.sp, color: ThemeUtil.getTheme().normalTextColor()),
          ),
        ),
      );
    }, dividingLine: () {
      return line1;
    }, divideType: DivideType.MIDDLE);
  }

  Widget buildCancelItem() {
    return GestureDetector(
      onTap: () {
        callback?.call(cancelStr);
        GetHelper.back();
      },
      child: Container(
        color: Colors.transparent,
        alignment: Alignment.center,
        width: double.infinity,
        height: 165.w,
        child: AutoSizeText(
          cancelStr,
          style: TextStyle(fontSize: 30.sp, color: ThemeUtil.getTheme().normalTextColor()),
        ),
      ),
    );
  }
}
